<?php

namespace addons\TfFutures\backend\controllers;

use addons\TfFutures\common\models\;
use Yii;
use common\models\common\PayWay;
use common\models\common\PayWaySearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;

/**
 * PayWayController implements the CRUD actions for PayWay model.
 */
class PayWayController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all PayWay models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new PayWaySearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single PayWay model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new PayWay model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */

    //添加新方式
    public function actionCreate()
    {
        //接收图片模型
        $model = new UploadForm();
        $model->load(Yii::$app->request->post());
        if (Yii::$app->request->isPost) {
            if (empty($model->content)) {
                \yii::$app->session->setFlash('error', '图片:请提交图片');
                return $this->redirect('index');
            }
            $model->imageFile = UploadedFile::getInstance($model, 'imageFile');
            //获取地址
            $href = $model->uploadIcon();
            if ($href == false) {
                \yii::$app->session->setFlash('error', '支付图片:提交失败');
                return $this->redirect('index');
            }
            //图片上传成功
            $PayWay = new PayWay();
            $PayWay->href = serialize($href);
            $PayWay->name = $model->content;
            if ($PayWay->save()) {
                \yii::$app->session->setFlash('success', '支付方式保存成功');
                return $this->redirect('index');
            }
            \yii::$app->session->setFlash('error', '支付方式信息提交失败');
            return $this->redirect('index');
        }
        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing PayWay model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing PayWay model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the PayWay model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return PayWay the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = PayWay::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
